home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0429.dms / q0429.adf / libray / libtext / noise.c < prev    next >
C/C++ Source or Header  |  1991-08-08  |  8KB  |  287 lines

  1. /*
  2.  * noise.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Robert Skinner, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: noise.c,v 4.0 91/07/17 14:43:38 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    noise.c,v $
  19.  * Revision 4.0  91/07/17  14:43:38  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "libcommon/common.h"
  24.  
  25. #define MINX        -1000000
  26. #define MINY        MINX
  27. #define MINZ        MINX
  28.  
  29. #define SCURVE(a) ((a)*(a)*(3.0-2.0*(a)))
  30. #define REALSCALE ( 2.0 / 65536.0 )
  31. #define NREALSCALE ( 2.0 / 4096.0 )
  32. #define Hash3d(a,b,c) hashTable[hashTable[hashTable[(a) & 0xfff] ^ ((b) & 0xfff)] ^ ((c) & 0xfff)]
  33. #define Hash(a,b,c) (xtab[(xtab[(xtab[(a) & 0xff] ^ (b)) & 0xff] ^ (c)) & 0xff] & 0xff)
  34.  
  35. #define INCRSUM(m,s,x,y,z)    ((s)*(RTable[m]*0.5        \
  36.                     + RTable[m+1]*(x)    \
  37.                     + RTable[m+2]*(y)    \
  38.                     + RTable[m+3]*(z)))    \
  39.  
  40.  
  41. #define MAXSIZE 267
  42.  
  43. Float        RTable[MAXSIZE];
  44. static short    *hashTable;
  45. static int    R(), Crc16();
  46.  
  47. static unsigned short xtab[256] =
  48. {
  49.    0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
  50.    0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
  51.    0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
  52.    0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
  53.    0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
  54.    0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
  55.    0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
  56.    0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
  57.    0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
  58.    0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
  59.    0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
  60.    0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
  61.    0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
  62.    0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
  63.    0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
  64.    0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
  65.    0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
  66.    0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
  67.    0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
  68.    0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
  69.    0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
  70.    0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
  71.    0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
  72.    0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
  73.    0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
  74.    0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
  75.    0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
  76.    0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
  77.    0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
  78.    0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
  79.    0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
  80.    0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
  81. };
  82.  
  83. Float Chaos(), Marble();
  84.  
  85. void
  86. InitTextureTable()
  87. {
  88.     int i, j, temp;
  89.  
  90.     seednrand(1);
  91.     hashTable = (short *) Malloc(4096*sizeof(short int));
  92.     for (i = 0; i < 4096; i++)
  93.         hashTable[i] = i;
  94.     for (i = 4095; i > 0; i--) {
  95.         j = (int)(nrand() * 4096);
  96.         temp = hashTable[i];
  97.         hashTable[i] = hashTable[j];
  98.         hashTable[j] = temp;
  99.     }
  100. }
  101.  
  102. void
  103. NoiseInit()
  104. {
  105.     int i;
  106.     Vector rp;
  107.  
  108.     InitTextureTable();
  109.  
  110.     for (i = 0; i < MAXSIZE; i++) {
  111.            rp.x = rp.y = rp.z = (Float)i;
  112.            RTable[i] = R(&rp)*REALSCALE - 1.0;
  113.      }
  114. }
  115.  
  116. static int
  117. R(v)
  118. Vector *v;
  119. {
  120.     v->x *= .12345;
  121.     v->y *= .12345;
  122.     v->z *= .12345;
  123.  
  124.     return Crc16(v, sizeof(Vector));
  125. }
  126.  
  127. /*
  128.  * Note that passing a Float to Crc16 and interpreting it as
  129.  * an array of chars means that machines with different floating-point
  130.  * representation schemes will evaluate Noise(point) differently.
  131.  */
  132. static int
  133. Crc16(buf, count)
  134. register char *buf;
  135. register int  count;
  136. {
  137.     register unsigned int crc = 0;
  138.  
  139.     while (count--)
  140.         crc = (crc >> 8) ^ xtab[ (unsigned char) (crc ^ *buf++) ];
  141.  
  142.     return crc;
  143. }
  144.  
  145.  
  146. /*
  147.  * Robert Skinner's Perlin-style "Noise" function
  148.  */
  149. Float
  150. Noise3(point)
  151. Vector *point;
  152. {
  153.     register int    ix, iy, iz, jx, jy, jz;
  154.     Float        x, y, z;
  155.     Float    sx, sy, sz, tx, ty, tz;
  156.     Float    sum;
  157.     short    m;
  158.  
  159.  
  160.     /* ensures the values are positive. */
  161.     x = point->x - MINX; y = point->y - MINY; z = point->z - MINZ;
  162.  
  163.     /* its equivalent integer lattice point. */
  164.     ix = (int)x; iy = (int)y; iz = (int)z;
  165.     jx = ix+1; jy = iy + 1; jz = iz + 1;
  166.  
  167.     sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
  168.  
  169.     /* the complement values of sx,sy,sz */
  170.     tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
  171.  
  172.     /*
  173.      *  interpolate!
  174.      */
  175.     m = Hash3d( ix, iy, iz ) & 0xFF;
  176.     sum = INCRSUM(m,(tx*ty*tz),(x-ix),(y-iy),(z-iz));
  177.  
  178.     m = Hash3d( jx, iy, iz ) & 0xFF;
  179.     sum += INCRSUM(m,(sx*ty*tz),(x-jx),(y-iy),(z-iz));
  180.  
  181.     m = Hash3d( ix, jy, iz ) & 0xFF;
  182.     sum += INCRSUM(m,(tx*sy*tz),(x-ix),(y-jy),(z-iz));
  183.  
  184.     m = Hash3d( jx, jy, iz ) & 0xFF;
  185.     sum += INCRSUM(m,(sx*sy*tz),(x-jx),(y-jy),(z-iz));
  186.  
  187.     m = Hash3d( ix, iy, jz ) & 0xFF;
  188.     sum += INCRSUM(m,(tx*ty*sz),(x-ix),(y-iy),(z-jz));
  189.  
  190.     m = Hash3d( jx, iy, jz ) & 0xFF;
  191.     sum += INCRSUM(m,(sx*ty*sz),(x-jx),(y-iy),(z-jz));
  192.  
  193.     m = Hash3d( ix, jy, jz ) & 0xFF;
  194.     sum += INCRSUM(m,(tx*sy*sz),(x-ix),(y-jy),(z-jz));
  195.  
  196.     m = Hash3d( jx, jy, jz ) & 0xFF;
  197.     sum += INCRSUM(m,(sx*sy*sz),(x-jx),(y-jy),(z-jz));
  198.  
  199.     return sum;
  200.  
  201. }
  202.  
  203. /*
  204.  * Vector-valued "Noise"
  205.  */
  206. void
  207. DNoise3(point, result)
  208. Vector *point, *result;
  209. {
  210.     register int    ix, iy, iz, jx, jy, jz;
  211.     Float        x, y, z;
  212.     Float px, py, pz, s;
  213.     Float    sx, sy, sz, tx, ty, tz;
  214.     short    m;
  215.  
  216.     /* ensures the values are positive. */
  217.     x = point->x - MINX; y = point->y - MINY; z = point->z - MINZ;
  218.  
  219.     /* its equivalent integer lattice point. */
  220.     ix = (int)x; iy = (int)y; iz = (int)z;
  221.     jx = ix+1; jy = iy + 1; jz = iz + 1;
  222.  
  223.     sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
  224.  
  225.     /* the complement values of sx,sy,sz */
  226.     tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
  227.  
  228.     /*
  229.      *  interpolate!
  230.      */
  231.     m = Hash3d( ix, iy, iz ) & 0xFF;
  232.     px = x-ix;  py = y-iy;  pz = z-iz;
  233.     s = tx*ty*tz;
  234.     result->x = INCRSUM(m,s,px,py,pz);
  235.     result->y = INCRSUM(m+4,s,px,py,pz);
  236.     result->z = INCRSUM(m+8,s,px,py,pz);
  237.  
  238.     m = Hash3d( jx, iy, iz ) & 0xFF;
  239.     px = x-jx;
  240.     s = sx*ty*tz;
  241.     result->x += INCRSUM(m,s,px,py,pz);
  242.     result->y += INCRSUM(m+4,s,px,py,pz);
  243.     result->z += INCRSUM(m+8,s,px,py,pz);
  244.  
  245.     m = Hash3d( jx, jy, iz ) & 0xFF;
  246.     py = y-jy;
  247.     s = sx*sy*tz;
  248.     result->x += INCRSUM(m,s,px,py,pz);
  249.     result->y += INCRSUM(m+4,s,px,py,pz);
  250.     result->z += INCRSUM(m+8,s,px,py,pz);
  251.  
  252.     m = Hash3d( ix, jy, iz ) & 0xFF;
  253.     px = x-ix;
  254.     s = tx*sy*tz;
  255.     result->x += INCRSUM(m,s,px,py,pz);
  256.     result->y += INCRSUM(m+4,s,px,py,pz);
  257.     result->z += INCRSUM(m+8,s,px,py,pz);
  258.  
  259.     m = Hash3d( ix, jy, jz ) & 0xFF;
  260.     pz = z-jz;
  261.     s = tx*sy*sz;
  262.     result->x += INCRSUM(m,s,px,py,pz);
  263.     result->y += INCRSUM(m+4,s,px,py,pz);
  264.     result->z += INCRSUM(m+8,s,px,py,pz);
  265.  
  266.     m = Hash3d( jx, jy, jz ) & 0xFF;
  267.     px = x-jx;
  268.     s = sx*sy*sz;
  269.     result->x += INCRSUM(m,s,px,py,pz);
  270.     result->y += INCRSUM(m+4,s,px,py,pz);
  271.     result->z += INCRSUM(m+8,s,px,py,pz);
  272.  
  273.     m = Hash3d( jx, iy, jz ) & 0xFF;
  274.     py = y-iy;
  275.     s = sx*ty*sz;
  276.     result->x += INCRSUM(m,s,px,py,pz);
  277.     result->y += INCRSUM(m+4,s,px,py,pz);
  278.     result->z += INCRSUM(m+8,s,px,py,pz);
  279.  
  280.     m = Hash3d( ix, iy, jz ) & 0xFF;
  281.     px = x-ix;
  282.     s = tx*ty*sz;
  283.     result->x += INCRSUM(m,s,px,py,pz);
  284.     result->y += INCRSUM(m+4,s,px,py,pz);
  285.     result->z += INCRSUM(m+8,s,px,py,pz);
  286. }
  287.